package com.mj.alexarank.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.mj.alexarank.R;
import com.mj.alexarank.service.AlexarankService;
import com.mj.alexarank.util.NetUtils;
import com.mj.dollars.AppConnect;
import com.mj.dollars.api.DollarsAppWallManager;
import com.mj.dollars.api.DollarsBManager;
import com.mj.dollars.api.DollarsIManager;
import com.mj.dollars.api.listener.DollarsIListener;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
/**
 * Alexa网站排名
 * @author zhaominglei
 * @date 2015-3-21
 * 
 */
public class MainActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	public static final String APPKEY = "8ac65b1061ec1e8b4ebdebc5b98e5c9d";
	private AlexarankService alexarankService = new AlexarankService();
	private boolean isExit = false;
	private TimerTask timerTask;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private EditText domainnameText; //域名输入框
	private String domainname; //域名
	private Button queryBtn; //查询
	private ProgressBar queryProgressBar; //查询进度条
	private TextView resultView; //结果
	private Button appOffersButton; //推荐应用
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.miniAdLinearLayout);
		domainnameText = (EditText)findViewById(R.id.alexa_domainname_edt);
		queryBtn = (Button)findViewById(R.id.alexa_query_btn);
		queryProgressBar = (ProgressBar)findViewById(R.id.alexa_query_progressbar);
		resultView = (TextView)findViewById(R.id.alexa_result);
		appOffersButton = (Button)findViewById(R.id.appOffersButton);
		
		queryBtn.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		AppConnect.getInstance(this).setCrashReport(true);
		DollarsBManager.getInstance().init(this, APPKEY);
		DollarsBManager.getInstance().loadBannerAD(this, miniAdLinearLayout);
		DollarsIManager coverManager = DollarsIManager.getInstance();
		coverManager.init(this,APPKEY);
		coverManager.show(this,new DollarsIListener() {
			@Override
			public void onSuccess() {
//				Toast.makeText(getApplicationContext(), "插屏广告加载成功", Toast.LENGTH_LONG).show();
			}
			@Override
			public void onFailure() {
//				Toast.makeText(getApplicationContext(), "插屏广告加载失败", Toast.LENGTH_LONG).show();
			}
			@Override
			public void onClosed() {
//				Toast.makeText(MainActivity.this, "插屏广告关闭", Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.alexa_query_btn:
			domainname = domainnameText.getText().toString();
			if (domainname == null || domainname.equals("")) {
				Toast.makeText(getApplicationContext(), R.string.alexa_domainname_hint, Toast.LENGTH_SHORT).show();
				return;
			}
			resultView.setText("");
			
			getAlexarank();
			break;
			
		case R.id.appOffersButton:
			DollarsAppWallManager.getInstance().init(this, APPKEY);
			DollarsAppWallManager.getInstance().loadAd(this, APPKEY);
			break;
			
		default:
			break;
		}
	}
	
	private void getAlexarank() {
		if (NetUtils.isConnected(getApplicationContext())) {
			queryProgressBar.setVisibility(View.VISIBLE);
			new AlexarankAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public void onBackPressed() {
		if (isExit) {
			AppConnect.getInstance(this).close();
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	public class AlexarankAsyncTask extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			return alexarankService.getAlexarank(domainname);
		}
		
		@Override
		protected void onPostExecute(String result) {
			queryProgressBar.setVisibility(View.GONE);
			if (result != null && !result.equals("")) {
				resultView.setText(result);
			} else {
				Toast.makeText(MainActivity.this, R.string.alexa_error2, Toast.LENGTH_SHORT).show();
			}
		}
	}
}
